import shutil
import os
import sys
import pytest
import subprocess
from conf import settings

if __name__ == '__main__':
    # 将以下目录加到sys路径，方便导包，如from test.xx import xxx 可简化为from test import xxx
    sys.path.insert(0, str(settings.BASE_DIR / "tests"))
    sys.path.insert(0, str(settings.BASE_DIR / "utils"))
    sys.path.insert(0, str(settings.BASE_DIR / "api"))

    if os.path.exists(settings.ALLURE_RESULTS):
        shutil.rmtree(settings.ALLURE_RESULTS)
        if os.path.exists(settings.ALLURE_REPORTS):
            shutil.rmtree(settings.ALLURE_REPORTS)

    pytest.main()

    # 生成allure测试报告
    subprocess.call(f"allure generate {settings.ALLURE_RESULTS} -o {settings.ALLURE_REPORTS} --clean", shell=True)

    # 开打allure测试报告
    subprocess.call(f"allure open {settings.ALLURE_REPORTS}", shell=True)
